package com.xxl.sso.server.controller;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.icom.sso.util.RetKit;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.entity.PageData;
import com.xxl.sso.core.entity.ReturnT;
import com.xxl.sso.core.filter.XxlSsoWebFilter;
import com.xxl.sso.core.login.SsoWebLoginHelper;
import com.xxl.sso.core.user.XxlSsoUser;
import com.xxl.sso.server.core.model.Department;
import com.xxl.sso.server.core.model.SysUser;
import com.xxl.sso.server.service.DepartmentService;
import com.xxl.sso.server.service.SysUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	private static Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private DepartmentService departmentService;

	@RequestMapping("/findUser")
	@ResponseBody
	public PageData<SysUser> findUsers(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer rows, String deptId, String name, String login) {
		if (deptId == null || "10000".equals(deptId.trim())) {
			PageData<SysUser> result = sysUserService.pageFindUser(page, rows, null, name, login);
			return result;
		} else {
			PageData<SysUser> result = sysUserService.pageFindUser(page, rows, deptId, name, login);
			return result;
		}
	}

	@RequestMapping("/addUser")
	public String addUser(Model model) {
		// 获取所有的部门信息
		List<Department> list = departmentService.findDepts();
		model.addAttribute("depts", list);

		return "user/user_add";
	}

	@RequestMapping("/changPwd")
	public String changePwd(Model model, HttpServletRequest request, HttpServletResponse response) {
		XxlSsoUser xxlUser = SsoWebLoginHelper.loginCheck(request, response);

		SysUser sysUser = sysUserService.findUserByLoginId(xxlUser.getLoginid());

		model.addAttribute("user", sysUser);
		return "user/user_edit_pwd";
	}

	/**
	 * 修改用户信息
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/editInfo")
	public String editInfo(Model model, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		// 获取所有的部门信息
		List<Department> list = departmentService.findDepts();
		SysUser sysUser = sysUserService.findUserById(id);

		model.addAttribute("depts", list);
		model.addAttribute("user", sysUser);
		return "user/user_edit";
	}

	@RequestMapping("/saveEditInfo")
	@ResponseBody
	public RetKit<String> saveEditInfo(SysUser user) {
		logger.info("用户信息修改......." + user.getName());
		RetKit<String> ret = new RetKit<String>(null);

		if (user.getId() == 0) {
			ret.setFail();
			ret.setMsg("修改用户信息错误！");
			return ret;
		} else if (user.getDeptId() == 0) {
			ret.setFail();
			ret.setMsg("所属部门 不能为空！");
			return ret;
		}

		ret = sysUserService.saveEditInfo(user);

		return ret;
	}

	/**
	 * 修改用户密码
	 * 
	 * @return
	 */
	@RequestMapping("/savePwd")
	@ResponseBody
	public RetKit<String> editUserPassword(SysUser user, String loginId, String nowpassword1, String nowpassword2,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("用户密码修改.......");
		RetKit<String> ret = new RetKit<String>(null);

		if (loginId == null || loginId.trim().length() == 0) {
			ret.setFail();
			ret.setMsg("登录名不能为空！");
			logger.debug("用户密码为空.......");
			return ret;
		} else if (user.getPassword() == null || user.getPassword().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("原密码不能为空！");
			return ret;
		} else if (nowpassword1 == null || nowpassword1.trim().length() == 0) {
			ret.setFail();
			ret.setMsg("新密码不能为空！");
			return ret;
		} else if (!nowpassword1.equals(nowpassword2)) {
			ret.setFail();
			ret.setMsg("两次密码输入不相同！");
			return ret;
		}

		ret = sysUserService.editUserPassword(loginId, user.getPassword(), nowpassword2);

		// 修改成功后清除登录状态
		if (ret.getFlag()) {
			// logout
			SsoWebLoginHelper.logout(request, response);
			logger.info("用户密码修改成功.......");
		}

		return ret;
	}

	/**
	 * 保存新增用户信息
	 * 
	 * @param user
	 * @param password1
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/saveUser")
	@ResponseBody
	public RetKit<String> saveUser(SysUser user, String password1, String dept_id, HttpServletRequest request,
			HttpServletResponse response) {
		RetKit<String> ret = new RetKit<String>(null);

		if (user.getName() == null || user.getName().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("用户名不能为空！");
			return ret;
		} else if (user.getLoginId() == null || user.getLoginId().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("登录名不能为空！");
			return ret;
		} else if (user.getPassword() == null || user.getPassword().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("密码不能为空！");
			return ret;
		} else if (password1 == null || password1.trim().length() == 0 || !user.getPassword().equals(password1)) {
			ret.setFail();
			ret.setMsg("两次密码不一致！");
			return ret;
		} else if (dept_id == null || dept_id.trim().length() == 0) {
			ret.setFail();
			ret.setMsg("所属部门不能为空！");
			return ret;
		}

		user.setDeptId(Integer.valueOf(dept_id));

		// 将用户信息保存到数据库
		ret = sysUserService.saveUser(user);

		return ret;
	}

	@RequestMapping("/del")
	@ResponseBody
	public RetKit<String> delUser(HttpServletRequest request, HttpServletResponse response) {
		RetKit<String> ret = new RetKit<String>(null);
		String id = request.getParameter("id");

		if (id == null || id.trim() == "") {
			ret.setFail();
			ret.setMsg("删除用户出现错误！");
		}
		ret = sysUserService.delUser(id);
		return ret;
	}

	@RequestMapping("/reset")
	@ResponseBody
	public RetKit<String> resetPwd(HttpServletRequest request, HttpServletResponse response) {
		RetKit<String> ret = new RetKit<String>(null);

		String id = request.getParameter("id");

		if (id == null || id.trim() == "") {
			ret.setFail();
			ret.setMsg("重置用户密码出现错误！");
		}
		ret = sysUserService.resetPwd(id);

		return ret;
	}
}
